//
//  STCalendarKitItemCollectionViewCell.m
//  STNewTools
//
//  Created by stoneobs on 2017/7/20.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

#import "STCalendarKitItemCollectionViewCell.h"
@interface STCalendarKitItemCollectionViewCell()

@end
@implementation STCalendarKitItemCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configSubView];
    }
    return self;
}
- (void)setModel:(STCalendarKitDayModel *)model{

    _model = model;
    [self.titleButton setTitle:[NSString stringWithFormat:@"%ld",model.dayValue] forState:UIControlStateNormal];
    
    if (!model.isThisMonth && model.isExistSchedule) {
        [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.titleButton setBackgroundImage:[UIImage imageNamed:@"btn_existschedule"] forState:UIControlStateNormal];
        return;
       
    }
    if (!model.isThisMonth && !model.isExistSchedule) {
        [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.titleButton setBackgroundImage:nil forState:UIControlStateNormal];
        return;
        
    }
    
    //根据有无日程，是否选中，是否今天，是否这个月 一共有16情况需要处理
    //处理规则，今天是红色，选中是蓝色，今天+选中还是红色
    
    //是否这个月 - 42个按钮中，属于当前月份的  则是这个月
    if (model.isThisMonth) {
        
        if (model.isExistSchedule && model.isToday && model.isSelected) {
            [self.titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.titleButton setBackgroundImage:[UIImage imageNamed:@"btn_todays_schedule"] forState:UIControlStateNormal];
        }else if (model.isExistSchedule && model.isToday && !model.isSelected){
            
            [self.titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.titleButton setBackgroundImage:[UIImage imageNamed:@"btn_todays_schedule"] forState:UIControlStateNormal];
        }else if (model.isExistSchedule && !model.isToday && !model.isSelected){
            
            [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.titleButton setBackgroundImage:[UIImage imageNamed:@"btn_existschedule"] forState:UIControlStateNormal];
            
            
        }else if (model.isExistSchedule && !model.isToday && model.isSelected){
            
            [self.titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.titleButton setBackgroundImage:[UIImage imageNamed:@"btn_select_schedule"] forState:UIControlStateNormal];
            
            
        }else if (!model.isExistSchedule && model.isToday && model.isSelected){
            
            [self.titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.titleButton setBackgroundImage:[UIImage imageNamed:@"btn_todays_noschedule"] forState:UIControlStateNormal];
            
            
        }else if (!model.isExistSchedule && model.isToday && !model.isSelected){
            
            [self.titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.titleButton setBackgroundImage:[UIImage imageNamed:@"btn_todays_noschedule"] forState:UIControlStateNormal];
            
            
        }else if (!model.isExistSchedule && !model.isToday && !model.isSelected){
            
            [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.titleButton setBackgroundImage:nil forState:UIControlStateNormal];
            
            
        }else if (!model.isExistSchedule && !model.isToday && model.isSelected){
            
            [self.titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.titleButton setBackgroundImage:[UIImage imageNamed:@"btn_select_noschedule"] forState:UIControlStateNormal];
            
            
        }

    }
    
    //不是这个月，当前逻辑，不是这个月，点击的时候不会让其selected 而是 滑动到上一个月或者下一个月，在使之selected
    if (!model.isThisMonth) {

        if (model.isExistSchedule) {
            
            [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.titleButton setBackgroundImage:[UIImage imageNamed:@"btn_existschedule"] forState:UIControlStateNormal];
            
        }else{
        
            [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.titleButton setBackgroundImage:nil forState:UIControlStateNormal];
        }
        
    }
    

}
#pragma mark --SubView
- (void)configSubView{
    
    self.backgroundColor = [UIColor whiteColor];

    self.titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.titleButton.frame = CGRectMake(0, 0, itemWith, itemWith);
    self.titleButton.backgroundColor = [UIColor whiteColor];
    self.titleButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.titleButton setTitle:@"0" forState:UIControlStateNormal];
    [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.titleButton addTarget:self action:@selector(titleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.titleButton];
    
}
#pragma mark --Action Method
- (void)titleButtonAction:(UIButton*)sender{
    
    if (self.model.isThisMonth) {
        //只有属于这个月才会有点击成selsected 和！ selected ，非这个月的点击需要滑动到该月 然后响应点击事件
        self.model.isSelected = !self.model.isSelected;
    }

    if (self.buttonAction) {
        self.buttonAction(sender);
    }
    
}
@end
